package org.example.Server.service.user;


import lombok.extern.slf4j.Slf4j;
import org.example.Server.mapper.user.UserMapper;
import org.example.Server.model.user.Student;
import org.example.Server.model.user.UserDTO;
import org.example.Server.model.user.UserInfo;
import org.example.Server.model.user.UserVO;
import org.example.Server.utils.JwtUtils;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.HashMap;
import java.util.Map;

import static org.example.Server.utils.ExceptionEnum.*;

@Slf4j
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    public ResultAPI login(UserDTO user) {
        UserVO userVO = userMapper.getUserByPhonePwd(user);
        log.info("登录用户", userVO.toString());
        if (userVO != null) {
            try {
                Map<String, Object> claims = new HashMap<>();
                claims.put("id", userVO.getId());
//                claims.put("username",userVO.getUsername());
                claims.put("phone_num", userVO.getPhone_num());
                claims.put("user_type", userVO.getUser_type());
                String token = JwtUtils.generateJwt(claims);
                userVO.setToken(token);
            } catch (Exception e) {
                return ResultAPI.error(SIGNATURE_ERROR.getResultCode(), SIGNATURE_ERROR.getResultMsg());
            }
            userMapper.addLoginlastime(user);
            return ResultAPI.success(userVO);
        }
        return ResultAPI.error(LOGIN_ERROR.getResultCode(), LOGIN_ERROR.getResultMsg());
    }

    /**
     * 用户注册
     *
     * @param data
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ResultAPI register(UserDTO data) {
        UserDTO user = userMapper.getUserByPhonenum(data.getPhone_num());
        //该手机号尚未注册
        if (user == null) {
            try {
                userMapper.register(data);
            } catch (Exception e) {
                e.printStackTrace();
                return ResultAPI.error(SQL_ERROR.getResultCode(), SQL_ERROR.getResultMsg());
            }
            if (data.getUser_type().equals("student")) {
                Student u = userMapper.getUserByStuNumber(data.getStu_number());
                if (u != null) {
                    return ResultAPI.error(USER_NUMEXIST.getResultCode(), USER_NUMEXIST.getResultMsg());
                }
                userMapper.addStu(data);
                //填写老师批量选课表中的学号对应的id
                userMapper.updateSemStuId(data);
            }
            return ResultAPI.success();
        } else return ResultAPI.error(USER_EXIST.getResultCode(), USER_EXIST.getResultMsg());
    }

    @Transactional
    public ResultAPI updateUser(UserInfo user) {
//        if (user.getPassword() != null) {
//            String pwd = userMapper.getPasswordById(user.getId());
//            if (!pwd.equals(user.getPassword())) {
//                return ResultAPI.error("密码错误");
//            }
//        }
        userMapper.updateUser(user);
//        if (user.getStu_number() != null && user.getStu_number().length() > 0) {
//            userMapper.updateStu(user);
//        }
        return ResultAPI.success();
    }

    public ResultAPI getUser(Integer userId) {
        UserInfo userInfo = userMapper.getUserById(userId);
        return ResultAPI.success(userInfo);
    }
}
